package com.ss.android.download;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import com.ss.android.download.a.b;

import java.io.File;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@TargetApi(value = 3)
public class DownloadService extends Service {
    @SuppressLint(value = {"UseSparseArrays"})
    final Map c;
    final ExecutorService d;
    a a;
    g b;
    l e;
    HandlerThread f;
    Handler g;
    volatile int h;
    private t i;
    private AlarmManager j;
    private r k;
    private Handler.Callback l;

    public DownloadService() {
        this.c = new HashMap();
        ThreadPoolExecutor v1 = new ThreadPoolExecutor(5, 5, 10, TimeUnit.SECONDS, new LinkedBlockingQueue());
        v1.allowCoreThreadTimeOut(true);
        this.d = ((ExecutorService) v1);
        this.l = new m(this);
    }

    public static void a(Context arg2) {
        try {
            arg2.startService(new Intent(arg2, DownloadService.class));
        } catch (Exception v0) {
        }
    }

    private static void a(String arg3) {
        if (!TextUtils.isEmpty(((CharSequence) arg3))) {
            if (com.ss.android.download.aa.d) {
                Log.d("SsDownloadManager", "deleteFileIfExists() deleting " + arg3);
            }

            File v0 = new File(arg3);
            if (!v0.exists()) {
                return;
            }

            if (v0.delete()) {
                return;
            }

            Log.w("SsDownloadManager", "file: \'" + arg3 + "\' couldn\'t be deleted");
        }
    }

    final void a() {
        this.g.removeMessages(1);
        this.g.obtainMessage(1, this.h, -1).sendToTarget();
    }

    final boolean b() {
        boolean v9_2;
        long v6_1;
        Object v3;
        long v2_2;
        boolean v4_1;
        Exception v5;
        int v6 = 0;
        long v4 = 0L;
        int v15;
        com.ss.android.download.DownloadInfo.DownloadInfo$a v14;
        long v12 = this.i.a();
        long v8 = 9223372036854775807L;
        HashSet v11 = new HashSet(this.c.keySet());
        k v2 = com.ss.android.download.k.a(this.getApplicationContext());
        Cursor v7 = v2.a(com.ss.android.download.a.a.a, null, null, null, null);
        try {
            v14 = new com.ss.android.download.DownloadInfo.DownloadInfo$a(v2, v7);
            v15 = v7.getColumnIndexOrThrow("_id");
            v4 = v8;
            v6 = 0;
            while (true) {
                label_24:
                if (!v7.moveToNext()) {
                    break;
                }

                v8 = v7.getLong(v15);
                ((Set) v11).remove(Long.valueOf(v8));
                v3 = this.c.get(Long.valueOf(v8));
                if (v3 != null) {
                    v14.a(((DownloadInfo) v3));
                    if (com.ss.android.download.aa.d) {
                        Log.v("SsDownloadManager", "processing updated download " + ((DownloadInfo) v3).a + ", status: " + ((DownloadInfo) v3).j);
                    }
                } else {
                    v3 = v14.a(this, this.i, this.k, this.b);
                    this.c.put(Long.valueOf(((DownloadInfo)v3).a), v3);
                    if (com.ss.android.download.aa.d) {
                        Log.v("SsDownloadManager", "processing inserted download " + ((DownloadInfo)v3).a);
                    }
                }

                if (((DownloadInfo) v3).v) {
                    if (!TextUtils.isEmpty(((DownloadInfo) v3).w)) {
                        this.getContentResolver().delete(Uri.parse(((DownloadInfo) v3).w), null, null);
                    }

                    this.c.remove(Long.valueOf(((DownloadInfo) v3).a));
                    DownloadService.a(((DownloadInfo) v3).e);
                    v2.a(((DownloadInfo) v3).b());
                    this.b.a(((DownloadInfo) v3));
                    this.b.a(com.ss.android.download.g.b(((DownloadInfo) v3)));
                } else {
                    String v8_1 = ((DownloadInfo) v3).e;
                    if (((DownloadInfo) v3).e != null) {
                        File v9 = new File(v8_1);
                        if (((DownloadInfo) v3).j == 200 && !v9.exists()) {
                            com.ss.android.download.g.a(((DownloadInfo) v3).C).a(com.ss.android.download.g.b(((DownloadInfo) v3)));
                            com.ss.android.download.k.a(((DownloadInfo) v3).C).a(((DownloadInfo) v3).b());
                        }
                    }
                    v6 = v6 | (((DownloadInfo) v3).a(this.d) ? 1 : 0) | (((DownloadInfo) v3).a(this.e) ? 1 : 0);
                }

                if (com.ss.android.download.a.a.c(((DownloadInfo) v3).j)) {
                    v8 = 9223372036854775807L;
                } else {
                    if (((DownloadInfo) v3).j != 194) {
                        v8 = 0;
                    } else {
                        v8 = ((DownloadInfo) v3).a(v12);
                        if (v8 <= v12) {
                            v8 = 0;
                        } else {
                            v8 -= v12;
                        }
                    }
                }
                v4 = Math.min(v8, v4);
                continue;
            }
        } catch (Exception v2_1) {
            v2_1.printStackTrace();
        }

        if (v7 != null) {
            try {
                v7.close();
            } catch (Exception v2_1) {
                v2_1.printStackTrace();
            }
        }
        v9_2 = v6 > 0;
        v6_1 = v4;
        Iterator v3_2 = ((Set) v11).iterator();
        while (v3_2.hasNext()) {
            Object v2_4 = this.c.get(Long.valueOf(((Long) v3_2.next()).longValue()));
            if (((DownloadInfo) v2_4).j == 192) {
                ((DownloadInfo) v2_4).j = 490;
            }

            if (((DownloadInfo) v2_4).g != 0 && ((DownloadInfo) v2_4).e != null) {
                if (com.ss.android.download.aa.d) {
                    Log.d("SsDownloadManager", "deleteDownloadLocked() deleting " + ((DownloadInfo) v2_4).e);
                }

                DownloadService.a(((DownloadInfo) v2_4).e);
            }

            this.b.a(com.ss.android.download.g.b(((DownloadInfo) v2_4)));
            this.c.remove(Long.valueOf(((DownloadInfo) v2_4).a));
        }

        this.b.a(this.c.values());
        if (v6_1 > 0 && v6_1 < 9223372036854775807L) {
            if (com.ss.android.download.aa.c) {
                Log.v("SsDownloadManager", "scheduling start in " + v6_1 + "ms");
            }

            Intent v8_2 = new Intent("android.ss.intent.action.DOWNLOAD_WAKEUP");
            v8_2.setClass(this, DownloadHandlerService.class);
            int v5_1 = 1;
            int v2_5 = android.support.design.a.u == null || !android.support.design.a.u.b() ? 0 : 1;
            if (v2_5 != 0) {
                v5_1 = 0;
            }

            try {
                android.support.design.a.o().a(this.j, v5_1, v6_1 + v12, PendingIntent.getService(this, 0, v8_2, 1073741824));
            } catch (Throwable v2_3) {
            }
        }

        return v9_2;
    }

    protected void dump(FileDescriptor arg9, PrintWriter arg10, String[] arg11) {
        b v2 = new b(arg10, "  ");
        Map v3 = this.c;
        synchronized (v3) {
            ArrayList v0_1 = new ArrayList(this.c.keySet());
            Collections.sort(((List) v0_1));
            Iterator v4 = ((List) v0_1).iterator();
            while (v4.hasNext()) {
                Object v0_2 = this.c.get(v4.next());
                v2.println("DownloadInfo:");
                v2.b.append(v2.a);
                v2.c = null;
                v2.a("mId", Long.valueOf(((DownloadInfo) v0_2).a));
                v2.a("mLastMod", Long.valueOf(((DownloadInfo) v0_2).m));
                v2.a("mPackage", ((DownloadInfo) v0_2).n);
                v2.println();
                v2.a("mUri", ((DownloadInfo) v0_2).b);
                v2.println();
                v2.a("mMimeType", ((DownloadInfo) v0_2).f);
                String v5 = "mCookies";
                String v1 = ((DownloadInfo) v0_2).o != null ? "yes" : "no";
                v2.a(v5, v1);
                v5 = "mReferer";
                v1 = ((DownloadInfo) v0_2).q != null ? "yes" : "no";
                v2.a(v5, v1);
                v2.a("mUserAgent", ((DownloadInfo) v0_2).p);
                v2.println();
                v2.a("mFileName", ((DownloadInfo) v0_2).e);
                v2.a("mDestination", Integer.valueOf(((DownloadInfo) v0_2).g));
                v2.println();
                v2.a("mStatus", com.ss.android.download.a.a.d(((DownloadInfo) v0_2).j));
                v2.a("mCurrentBytes", Long.valueOf(((DownloadInfo) v0_2).s));
                v2.a("mTotalBytes", Long.valueOf(((DownloadInfo) v0_2).r));
                v2.println();
                v2.a("mNumFailed", Integer.valueOf(((DownloadInfo) v0_2).k));
                v2.a("mRetryAfter", Integer.valueOf(((DownloadInfo) v0_2).l));
                v2.a("mETag", ((DownloadInfo) v0_2).t);
                v2.println();
                v2.a("mAllowedNetworkTypes", Integer.valueOf(((DownloadInfo) v0_2).x));
                v2.a("mAllowRoaming", Boolean.valueOf(((DownloadInfo) v0_2).y));
                v2.println();
                v2.b.delete(0, v2.a.length());
                v2.c = null;
            }
            return;
        }
    }

    public IBinder onBind(Intent arg3) {
        throw new UnsupportedOperationException("Cannot bind to Download Manager Service");
    }

    @TargetApi(value = 3)
    public void onCreate() {
        super.onCreate();
        if (com.ss.android.download.aa.d) {
            Log.v("SsDownloadManager", "Service onCreate");
        }

        if (!android.support.design.a.u(this.getApplicationContext())) {
            this.stopSelf();
        } else if (!android.support.design.a.p()) {
            this.stopSelf();
        } else {
            if (this.i == null) {
                this.i = p.a(((Context) this));
            }

            this.j = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
            this.k = new r(this);
            this.f = new HandlerThread("SsDownloadManager-UpdateThread");
            this.f.start();
            this.g = new Handler(this.f.getLooper(), this.l);
            this.e = new l(this);
            this.b = com.ss.android.download.g.a(this);
            this.b.a();
            this.a = new a(this);
            this.getContentResolver().registerContentObserver(com.ss.android.download.a.a.a, true, this.a);
        }
    }

    @TargetApi(value = 5)
    public void onDestroy() {
        try {
            if (this.a != null) {
                this.getContentResolver().unregisterContentObserver(this.a);
            }

            if (this.e != null) {
                this.e.a.disconnect();
            }

            if (this.f != null) {
                this.f.quit();
            }

            this.d.shutdown();
            if (!com.ss.android.download.aa.d) {
//                goto label_21;
            } else {
                Log.v("SsDownloadManager", "Service onDestroy");
            }
        } catch (Exception v0) {
        }

//        label_21:
        super.onDestroy();
    }

    @TargetApi(value = 5)
    public int onStartCommand(Intent arg4, int arg5, int arg6) {
        int v0 = super.onStartCommand(arg4, arg5, arg6);
        if (com.ss.android.download.aa.d) {
            Log.v("SsDownloadManager", "Service onStart");
        }

        if (!android.support.design.a.u(this.getApplicationContext())) {
            this.stopSelf();
        } else if (!android.support.design.a.p()) {
            this.stopSelf();
        } else {
            this.h = arg6;
            this.a();
        }

        return v0;
    }

    final class a extends ContentObserver {
        private final DownloadService a;

        public a(DownloadService arg2) {
            super(new Handler());
            this.a = arg2;
        }

        public final void onChange(boolean arg2) {
            this.a.a();
        }
    }
}

